package com.literature.threeGroup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.literature.threeGroup.mapper.*;
import com.literature.threeGroup.pojo.Log;
import com.literature.threeGroup.pojo.Menu;
import com.literature.threeGroup.pojo.MenuRole;
import com.literature.threeGroup.pojo.Role;
import com.literature.threeGroup.service.MenuService;
import com.literature.threeGroup.service.RoleService;
import com.literature.threeGroup.util.IdWorker;
import com.literature.threeGroup.util.PageResult;
import com.literature.threeGroup.vo.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author VULCAN
 * @create 2020-01-07 14:30
 * @desc
 **/
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuRoleMapper menuRoleMapper;
    @Autowired
    private RoleUserMapper roleUserMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private LogMapper logMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
      * @name  添加角色
     * @Author VULCAN
     * @Date 2020/1/10 8:42
     */
    @Override
    public void insertrole(Role role) {
        roleMapper.insert(role);
    }

    /**
      * @name  角色列表
     * @Author VULCAN
     * @Date 2020/1/10 8:44
     */
    @Override
    public List<Role> findallrole() {
        return roleMapper.findallrole();
    }

    /**
      * @name  统计数量
     * @Author VULCAN
     * @Date 2020/1/11 10:00
     */
    @Override
    public List<Role> findallrolecount() {
        return roleMapper.findallrolecount();
    }
    /**
     * @name  分页 模糊
     * @Author VULCAN
     * @Date 2020/1/10 19:59
     */
    @Override
    public PageResult findallrolenamecount(Role role , int pageNum , int pageSize ) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Role> findlog = (Page<Role>) roleMapper.findallrolenamecount(role);
        return new PageResult(findlog.getTotal(),findlog.getResult());
    }
    /**
     * @name  批量删除
     * @Author VULCAN
     * @Date 2020/1/17 9:47
     */
    @Override
    public String rolebulkdel(String[] ids) throws UnknownHostException {
        ArrayList<Object> list = new ArrayList<>();
        for (String id : ids) {
            list.add(id);
            //删除该角色
            roleMapper.roledel(id+"");
            //删除该角色所赋权限
            menuRoleMapper.deletemenuRole(id);
        }
        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("角色批量删除-"+list+"！！");
        log.setLogType("角色批量删除");
        logMapper.insert(log);
        return null;
    }
    /**
      * @name  角色添加
     * @Author VULCAN
     * @Date 2020/1/17 12:45
     */
    @Override
    public void rolebulkadd(String[] ids, Role role) throws UnknownHostException {
        //添加角色
        role.setRoleId(idWorker.nextId()+"");
        role.setRoleDate(new Date());
        roleMapper.insert(role);
        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("角色添加-"+role.getRoleName()+"！！");
        log.setLogType("角色添加");
        logMapper.insert(log);

        for (String id : ids) {
            //给角色赋权限
            MenuRole menuRole = new MenuRole();
            menuRole.setRoleId(role.getRoleId());
            menuRole.setMenuId(id);
            menuRoleMapper.insert(menuRole);
        }

    }
    /**
      * @name  根据id查询
     * @Author VULCAN
     * @Date 2020/1/21 19:14
     */
    @Override
    public Role findbyroleid(String roleId) {
        return roleMapper.findbyroleid(roleId);
    }

//    @Override
//    public void roleupdate(Role role) {
//        roleMapper.roleupdate(role);
//    }
    /**
      * @name  权限角色中间表修改
     * @Author VULCAN
     * @Date 2020/1/21 19:14
     */
    @Override
    public void rolemenuupdate(MenuRole menuRole) {
        menuRoleMapper.rolemenuupdate(menuRole);
    }
    /**
      * @name  权限角色根据id查询
     * @Author VULCAN
     * @Date 2020/1/21 19:13
     */
    @Override
    public List<MenuRole> findbyrolemenuid(String roleId) {
        return menuRoleMapper.findbyrolemenuid(roleId);
    }
    /**
      * @name  角色修改
     * @Author VULCAN
     * @Date 2020/1/21 19:13
     */
    @Override
    public void roleupdate(String[] ids, Role role) throws UnknownHostException {
        //修改角色
        role.setRoleDate(new Date());
        roleMapper.roleupdate(role);
        //删除该角色所赋权限
        menuRoleMapper.deletemenuRole(role.getRoleId());
        for (String id : ids) {
            //给角色赋权限
            MenuRole menuRole = new MenuRole();
            menuRole.setRoleId(role.getRoleId());
            menuRole.setMenuId(id);
            menuRoleMapper.insert(menuRole);
        }
        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("角色修改-"+role.getRoleName()+"！！");
        log.setLogType("角色修改");
        logMapper.insert(log);
    }
    /**
      * @name  rolecount
     * @Author VULCAN
     * @Date 2020/1/28 14:28
     */
    @Override
    public List rolecount() {
        return roleMapper.rolecount();
    }
}
